<template>
    <el-dialog :title="showTitle" @close="close" :visible="showDialog" width="50%">
        <el-form ref="addDept" :model="formData" :rules="rules" label-width="120px">
            <el-form-item prop="name" label="部门名称">
                <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%;" size="mini"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="部门编码">
                <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%;" size="mini"></el-input>
            </el-form-item>
            <el-form-item prop="managerId" label="部门负责人">
                <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 80%;" size="mini">
                    <!-- 下拉选项 循环 负责人的数据 label显示的字段  value存储的字段 -->
                    <el-option v-for="item in managerList" :key="item.id" :label="item.username"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="introduce" label="部门介绍">
                <el-input v-model="formData.introduce" placeholder="1-100个字符" type='textarea' size="mini"
                    style="width: 80%;"></el-input>
            </el-form-item>
            <el-form-item>
                <el-row type="flex" justify="center">
                    <el-col :span="12">
                        <el-button type="primary" size="mini" @click="btnOk">确认</el-button>
                        <el-button size="mini" @click="close">取消</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import { delDepartment, updateDepartment, getDepartmentDetail, getDepartment, getManagerList, addDepartment } from '@/api/department'

export default {
    components: {},
    props: {
        showDialog: {
            type: Boolean,
            default: false,
        },
        currentNodeId: {
            type: Number,
            default: null,
        },
    },
    data() {
        return {
            managerList: [],//存储负责人数据
            formData: {
                code: '',
                introduce: '',
                managerId: '',
                name: '',
                pid: '',
            },
            rules: {
                code: [
                    { required: true, message: '请输入部门编码', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
                    {
                        trigger: 'blur',
                        // 重复
                        validator: async (rule, value, callback) => {
                            let result = await getDepartment()
                            if (this.formData.id) {
                                result = result.filter(item => item.id !== this.formData.id)
                            }
                            if (result.some(item => item.code === value)) {
                                callback(new Error('部门编码重复'))
                            } else {
                                callback()
                            }
                        }
                    }
                ],
                introduce: [
                    { required: true, message: '请输入部门介绍', trigger: 'blur' },
                    { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
                ],
                managerId: [
                    { required: true, message: '请选择部门负责人', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入部门名称', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' },
                    {
                        trigger: 'blur',
                        // 重复
                        validator: async (rule, value, callback) => {
                            let result = await getDepartment()
                            if (this.formData.id) {
                                result = result.filter(item => item.id !== this.formData.id)
                            }
                            if (result.some(item => item.name === value)) {
                                callback(new Error('部门名称重复'))
                            } else {
                                callback()
                            }
                        }
                    }
                ]
            }
        }
    },
    computed: {
        showTitle() {
            return this.formData.id ? '修改部门' : '新增部门'
        }
    },
    methods: {
        close() {
            //修改父组件的值 淄川付
            this.formData = {
                code: '',
                introduce: '',
                managerId: '',
                name: '',
                pid: '',
            }
            this.$refs.addDept.resetFields()
            this.$emit('update:showDialog', false)
        },
        async getManagerList() {
            this.managerList = await getManagerList()
        },
        btnOk() {
            this.$refs.addDept.validate(async isOK => {
                if (isOK) {
                    let msg = '新增'
                    if (this.formData.id) {
                        //修改部门
                        msg = '更新'
                        await updateDepartment(this.formData)
                    } else {
                        //新增部门
                        await addDepartment({ ...this.formData, pid: this.currentNodeId })
                    }

                    //通知父组件刷新数据
                    this.$emit('updateDepartment')
                    //提示
                    this.$message({
                        message: `${msg}成功`,
                        type: 'success'
                    })
                    //关闭弹窗
                    this.close()
                }
            })
        },
        //获取部门详情
        async getDepartmentDetail() {
            this.formData = await getDepartmentDetail(this.currentNodeId)
        }
    },
    created() {
        this.getManagerList()
    },
    mounted() {

    },
}
</script>
<style scoped></style>